package org.example.studentsystem_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Notifications;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.service.NotificationsService;
import org.example.studentsystem_back.mapper.NotificationsMapper;
import org.example.studentsystem_back.vo.notificitionsVo.NotificationsIdVo;
import org.example.studentsystem_back.vo.notificitionsVo.NotificationsVo;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 马鑫
* @description 针对表【notifications】的数据库操作Service实现
* @createDate 2024-07-09 21:38:41
*/
@Service
public class NotificationsServiceImpl extends ServiceImpl<NotificationsMapper, Notifications>
    implements NotificationsService{
    @Resource
    NotificationsMapper notificationsMapper;
    @Override
    public CommonResponse<NotificationsVo> getAllNotifications(String category, PageDTO pageDTO) {
        Page<Notifications> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        QueryWrapper<Notifications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", category);
        List<Notifications> notifications = notificationsMapper.selectPage(page,queryWrapper).getRecords();
        int count = Math.toIntExact(notificationsMapper.selectCount(queryWrapper));
        NotificationsVo notificationsVo = new NotificationsVo();
        List<NotificationsIdVo> notificationsIdVos = new ArrayList<>();
        int i=(pageDTO.getPage()*10)-9;
        for (Notifications notification : notifications) {
            NotificationsIdVo notificationsIdVo = new NotificationsIdVo();
            notificationsIdVo.setNotifications(notification);
            notificationsIdVo.setSuiId(i);
            i++;
            notificationsIdVos.add(notificationsIdVo);
        }
        notificationsVo.setNotificationsIdVos(notificationsIdVos);
        notificationsVo.setCount(count);
        return CommonResponse.createForSuccess("查询成功", notificationsVo);
    }

    @Override
    public CommonResponse<NotificationsVo> getNotificationsSearch(String Title, String department, String date1, String date2,String category,PageDTO pageDTO) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Page<Notifications> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        QueryWrapper<Notifications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", category).like("title", Title).like("issuing_department",department);
        if(date1!=null&&date2!=null) {
            date1=date1.substring(0,10);
            date2=date2.substring(0,10);
            Date date3 = sdf.parse(date1);
            Date date4 = sdf.parse(date2);
            queryWrapper.ge("sent_date", date3);
            queryWrapper.le("sent_date", date4);
        }
        queryWrapper.orderByDesc("sent_date");
        List<Notifications> notifications = notificationsMapper.selectPage(page,queryWrapper).getRecords();
        int count = Math.toIntExact(notificationsMapper.selectCount(queryWrapper));
        NotificationsVo notificationsVo = new NotificationsVo();
        List<NotificationsIdVo> notificationsIdVos = new ArrayList<>();
        int i=(pageDTO.getPage()*10)-9;
        for (Notifications notification : notifications) {
            NotificationsIdVo notificationsIdVo = new NotificationsIdVo();
            notificationsIdVo.setNotifications(notification);
            notificationsIdVo.setSuiId(i);
            String formattedDate = sdf.format(notification.getSentDate());
            notificationsIdVo.setYingDate(formattedDate);
            i++;
            notificationsIdVos.add(notificationsIdVo);
        }
        notificationsVo.setNotificationsIdVos(notificationsIdVos);
        notificationsVo.setCount(count);
        return CommonResponse.createForSuccess("查询成功",notificationsVo);
    }

}




